package iscript.gameFramework.GameLite.thread 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.clearInterval;
	import flash.utils.clearTimeout;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;
	import iscript.gameFramework.GameLite.Game;
	public class GameThread extends EventDispatcher
	{
		
		public function GameThread() 
		{
			
		}
		

		
		/**
		 * WHEN   
		 * @param	object
		 * @param	param
		 * @param	value
		 * @param	sleep
		 * @param	callback
		 * @param	...arg
		 */
		static public function when(object:*, param:String,value:*,callback:Function, ...arg:*):void {			    
			   _whenLogic(false, false, object, param,value,callback, arg);
			}
          
		static private function _whenLogic(loop:Boolean, isnot:Boolean, object:*, param:String, value:*,callback:Function,...arg:*):void {
			   	var  id:uint = setInterval(function():void {
					                  var result:Boolean;
									  if (isnot) {
										  result = object[param] != value 
										  } else {
										  result = object[param] == value 
										  }					                  
									  if (result) {
										  if (!loop) { clearInterval(id) }
										   if (arg[0].length==0 && arg[0][0]==null) {
												callback.call();
											  } else {										
												callback(arg)
											  }; 
										  }
								}, 1000/Game.graphicsDeviceManager.fps);
			
			}
		/**
		 * WHENNOT
		 * @param	object
		 * @param	param
		 * @param	value
		 * @param	sleep
		 * @param	callback
		 * @param	...arg
		 */	
		static public function whenNot(object:*, param:String,value:*,callback:Function, ...arg):void {			    
				 _whenLogic(false, true, object, param,value,callback, arg);
			}	
			
		
		
	}

}

